/*
 * File: CoreService.java
 * 
 * Change Log
 * Author				DTG				Description of Change
 * ---------------------------------------------------------------------------
 * Timothy M. Lemm		17OCT11 2016	Initial Service Structure
 * 										Accepting Binds Support added
 * 										Create Phidget Service added
 * 
 * Timothy M. Lemm		18OCT11 0737	Create Control Service added 
 * 
 * Timothy M. Lemm		19OCT11 1242	Initial USB Support added	
 */
package com.erau.droidsatframework.core;

import java.util.HashMap;
import java.util.Iterator;

import com.erau.droidsatframework.control.ControlAlgorithmService;
import com.erau.droidsatframework.control.ControlService;
import com.erau.droidsatframework.phidget.PhidgetService;

import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;

public class CoreService extends Service {
	
	//Services
	private final IBinder localBinder = new CoreBinder(); //For dealing with incoming binds	
	
	Intent controlIntent = new Intent(this, ControlService.class);
	Intent controlAlgoIntent = new Intent(this, ControlAlgorithmService.class);
	Intent phidgetIntent = new Intent(this, PhidgetService.class);
	
	//Service Flags
	boolean controlStarted = false;
	boolean controlAlogrithmStarted = false;
	boolean phidgetStarted = false;
	
	
	//METHOD: onBind
	//Android specific method that returns an IBinder which allows
	//the clients that are binding this service to interface.
	@Override
	public IBinder onBind(Intent arg0) {
		return localBinder; //Return the IBinder to the system to allow binding
	}//End onBind()
	
	//CLASS: CoreBinder
	//Class that contains the information for the CoreService... 
	//We have a method so that multiple things can bind into us...
	public class CoreBinder extends Binder {
		public CoreService getService() {
			return CoreService.this;
		}//End getService
	}//End CoreBinder
	
	@Override
	public void onCreate() {
		startService(controlIntent);
		startService(phidgetIntent);
	}//End onCreate

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		return START_NOT_STICKY; //We can die
	}//End onStartCommand
	
	@Override
	public void onDestroy(){
		if(controlAlogrithmStarted){
			stopService(controlAlgoIntent);
		}
		stopService(controlIntent);
		stopService(phidgetIntent);
	}
	
	public void startAlgorithm(){
		if(controlAlogrithmStarted = false){
			if(startService(controlAlgoIntent) != null ){
				controlAlogrithmStarted = true;
			}
		}
	}
	
	public void stopAlgorthim(){
		if(controlAlogrithmStarted = true){
			stopService(controlAlgoIntent);
			controlAlogrithmStarted = false;
		}
	}
	
}
	
	
	
	
	
	